package com.softwinner.bt.policy;

import com.softwinner.bt.MainActivity;
import com.softwinner.bt.CallDialog;
import com.softwinner.bt.R;
import com.softwinner.bt.service.BtService;
import com.softwinner.bt.util.BTConfig;
import com.softwinner.bt.util.Command;
import com.softwinner.bt.util.Utils;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Gpio;
import android.os.Message;
import android.widget.Toast;
import android.util.Log;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import com.softwinner.bt.util.BTLog;
public class VoiceReceiver extends BroadcastReceiver{
	private static final String TAG ="VoiceReceiver";
	Context mContext;
	String number;
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub	
		String action = intent.getAction();
		mContext = context;
		if(action.equals("action.intent.AIOS_DIAL")){//打电话，需要发接通中的消息出去
			if(BtService.mBtStatus != BTManager.STATE_CONNECTED){
				openBt();
				Toast.makeText(context, R.string.msg_unconnectedorcalling, 1000).show();
				return;
			}
			String phoneNumber = intent.getStringExtra("number");
			Log.i("zhu","shou dao AIOS_DIAL number====="+phoneNumber);
			BTManager.getInstance().sendCommand(Command.SEND_MAKECALL + phoneNumber);
			//getPendingIntent(phoneNumber,CallDialog.class);
		}else if (action.equals("action.intent.AIOS_ACCEPT")) {//接听
			BTManager.getInstance().sendCommand(Command.SEND_ANSWER);
			Log.i("zhu","show dao AIOS_ACCEPT=====");
		}else if (action.equals("action.intent.AIOS_REJECT")) {//拒接
			BTManager.getInstance().sendCommand(
					Command.SEND_HANG_UP);
			Log.i("zhu","show dao AIOS_REJECT=====");
		}
	}
	private void getPendingIntent(String num,int what,Class<?> cls) {
        Intent localIntent = new Intent();
        Bundle localBundle = new Bundle();
        localBundle.putString("number", num);
        localBundle.putInt("what", what);
        localIntent.putExtras(localBundle);
        localIntent.setClass(mContext, cls);
        if(isBtAppTop()){
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }else{
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        }
                mContext.startActivity(localIntent);
    }
        public boolean isBtAppTop(){
        ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
                RunningTaskInfo info = manager.getRunningTasks(1).get(0);
                String pkgName = info.topActivity.getPackageName();
                BTLog.v(getClass(), "isBtAppTop() topPkgName=" + pkgName);
                if (pkgName.equals("com.softwinner.bt")){
                        return true;
                }
                return false;
    }
	public void onEcarReceiver(Intent intent, int cmd) {
		if ((BTConfig.HAND_UP_CMD & cmd) == 1) {
			// Hangup
			BTManager.getInstance().sendCommand(
					Command.SEND_HANG_UP);
		}
		if ((BTConfig.MAKE_CALL_CMD & cmd) == 2) {
			// call
			if(BtService.mBtStatus != BTManager.STATE_CONNECTED){
				openBt();
				Toast.makeText(mContext, R.string.msg_unconnectedorcalling, 1000).show();
				return;
			}
			String phoneNumber = intent.getStringExtra(BTConfig.ECAR_CALL_NUMBER);
			BTManager.getInstance().sendCommand(Command.SEND_SET_DTMFNUM + phoneNumber);
			BTManager.getInstance().sendCommand(Command.SEND_MAKECALL + phoneNumber);
		}

		if ((BTConfig.SEND_NAVI_CMD & cmd) == 4) {
			//
		}

		if ((BTConfig.SPEAKER_DISABLE_CMD & cmd) == 8) {
			// disable spk
			//Gpio.writeGpio('H', 15, 0);
		}

		if ((BTConfig.SPEAKER_ENABLE_CMD & cmd) == 16) {
			// enable spk
			//Gpio.writeGpio('H', 15,1);
		}
	}
	/**/
	public void openBt(){
		Intent intent = new Intent(Intent.ACTION_MAIN);		
		intent.setClass(mContext, MainActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(intent);
		mContext.sendBroadcast(new Intent(Intent.ACTION_DISABLE_FULLSCREEN));
	}

}
